package com.tjbank.cssys.service.impl;

import java.util.List;

import com.tjbank.cssys.framework.base.util.GeneratIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tjbank.cssys.framework.base.util.GeneratIDUtil;
import com.tjbank.cssys.api.RelationAddService;
import com.tjbank.cssys.dto.RoleRelationDTO;
import com.tjbank.cssys.dto.UserDTO;
import com.tjbank.cssys.mybatis.IamRelationshipDao;
import com.tjbank.cssys.mybatis.entity.IamRelationship;
import com.tjbank.cssys.mybatis.entity.IamUser;
import com.tjbank.cssys.mybatis.sqlbuilder.IamRelationshipSqlBuilder;
import com.tjbank.cssys.service.enums.EntityTypeEnum;
import com.tjbank.cssys.service.enums.RelaCodeEnum;

/**
 * 新增关联关系
 * 
 * Package : com.tansun.magicube.iam.service.impl
 * 
 * @author -- lijiangtao 2020年5月20日 下午2:11:48
 *
 */
@Service
public class RelationAddServiceImpl implements RelationAddService {

	@Autowired
	private IamRelationshipDao relationshipDao;

	/**
	 * 新增用户与角色关联关系
	 * 
	 * @param userDTO
	 * @param entity
	 * @author -- lijiangtao 2020年6月20日 下午12:31:33
	 */
	@Transactional
	public void addURRelation(UserDTO userDTO, IamUser entity) {
		userDTO.getRoleIdList().forEach(roleId -> {
			IamRelationship relationship = new IamRelationship();
			relationship.setId(GeneratIDUtil.getUUId());
			relationship.setEntity1Type(EntityTypeEnum.USER_TYPE.getCode());
			relationship.setEntity1Code(entity.getId());
			relationship.setRelaCode(RelaCodeEnum.USER_ROLE.getCode());
			relationship.setEntity2Type(EntityTypeEnum.ROLE_TYPE.getCode());
			relationship.setEntity2Code(roleId);
			relationshipDao.insert(relationship);
		});
	}

	/**
	 * 新增用户与岗位关联关系
	 * 
	 * @param userDTO
	 * @param entity
	 * @author -- lijiangtao 2020年6月18日 下午8:14:55
	 */
	@Transactional
	public void addUPRelationship(UserDTO userDTO, IamUser entity) {
		userDTO.getPostIdList().forEach(postId -> {
			IamRelationship relationship = new IamRelationship();
			relationship.setId(GeneratIDUtil.getUUId());
			relationship.setEntity1Type(EntityTypeEnum.USER_TYPE.getCode());
			relationship.setEntity1Code(entity.getId());
			relationship.setRelaCode(RelaCodeEnum.USER_POST.getCode());
			relationship.setEntity2Type(EntityTypeEnum.POST_TYPE.getCode());
			relationship.setEntity2Code(postId);
			relationshipDao.insert(relationship);
		});
	}

	/**
	 * 删除当前用户下所有角色
	 * 
	 * @param userId
	 * @author -- lijiangtao 2020年5月20日 下午4:24:46
	 */
	@Override
	@Transactional
	public void deleteRoleByUserId(String userId) {
		IamRelationshipSqlBuilder sqlBuilder = new IamRelationshipSqlBuilder();
		sqlBuilder.andEntity1CodeEqualTo(userId).andRelaCodeEqualTo(RelaCodeEnum.USER_ROLE.getCode());
		relationshipDao.deleteBySqlBuilder(sqlBuilder);
	}

	/**
	 * 删除当前用户下所有岗位
	 * 
	 * @param userId
	 * @author -- lijiangtao 2020年6月20日 下午12:38:48
	 */
	@Transactional
	public void deleteUPRelationship(String userId) {
		IamRelationshipSqlBuilder sqlBuilder = new IamRelationshipSqlBuilder();
		sqlBuilder.andEntity1CodeEqualTo(userId).andRelaCodeEqualTo(RelaCodeEnum.USER_POST.getCode());
		relationshipDao.deleteBySqlBuilder(sqlBuilder);
	}

	/**
	 * 新增角色与权限关联关系
	 * 
	 * @param roleRelationDTO
	 * @author -- lijiangtao 2020年7月24日 下午5:20:34
	 */
	@Override
	@Transactional
	public void addRP(RoleRelationDTO roleDTO) {
		this.deletePermissionByRoleId(roleDTO.getRoleId());
		this.add_Role_MenuPerm(roleDTO.getRoleId(), roleDTO.getMenuPermList());
		this.add_Role_DataPerm(roleDTO.getRoleId(), roleDTO.getDataPermList());
		this.add_Role_APIPerm(roleDTO.getRoleId(), roleDTO.getApiPermList());
	}

	/**
	 * 删除角色下所有权限
	 * 
	 * @param roleIds
	 * @author -- lijiangtao 2020年5月23日 下午5:49:06
	 */
	@Override
	@Transactional
	public void deletePermissionByRoleId(String roleId) {
		IamRelationshipSqlBuilder sqlBuilder = new IamRelationshipSqlBuilder();
		sqlBuilder.andEntity1CodeEqualTo(roleId);
		relationshipDao.deleteBySqlBuilder(sqlBuilder);
	}

	/**
	 * 菜单权限
	 * 
	 * @param roleRelationDTO
	 * @author -- lijiangtao 2020年7月24日 下午5:25:38
	 */
	private void add_Role_MenuPerm(String roleId, List<String> menuPermissionList) {
		if (!menuPermissionList.isEmpty()) {
			menuPermissionList.forEach(e -> {
				IamRelationship relationship = setRelationship(roleId, e);
				relationship.setRelaCode(RelaCodeEnum.ROLE_MENU_PERM.getCode());
				relationshipDao.insert(relationship);
			});
		}
	}

	/**
	 * 数据权限 ---->机构向下
	 * 
	 * @param roleId
	 * @param dataPermissionList
	 * @author -- lijiangtao 2020年7月24日 下午5:27:32
	 */
	private void add_Role_DataPerm(String roleId, List<String> dataPermList) {
		if (!dataPermList.isEmpty()) {
			dataPermList.forEach(e -> {
				IamRelationship relationship = setRelationship(roleId, e);
				relationship.setRelaCode(RelaCodeEnum.ROLE_DATA_PERM.getCode());
				relationshipDao.insert(relationship);
			});
		}
	}

	/**
	 * 接口权限
	 * 
	 * @param roleId
	 * @param apiPermissionList
	 * @author -- lijiangtao 2020年7月24日 下午5:27:39
	 */
	private void add_Role_APIPerm(String roleId, List<String> apiPermList) {
		if (!apiPermList.isEmpty()) {
			apiPermList.forEach(e -> {
				IamRelationship relationship = setRelationship(roleId, e);
				relationship.setRelaCode(RelaCodeEnum.ROLE_API_PERM.getCode());
				relationshipDao.insert(relationship);
			});
		}
	}

	/**
	 * setRelationship
	 * 
	 * @param roleId
	 * @param e
	 * @return
	 * @author -- lijiangtao 2020年7月24日 下午5:42:11
	 */
	private IamRelationship setRelationship(String roleId, String e) {
		IamRelationship relationship = new IamRelationship();
		relationship.setId(GeneratIDUtil.getUUId());
		relationship.setEntity1Type(EntityTypeEnum.ROLE_TYPE.getCode());
		relationship.setEntity1Code(roleId);
		relationship.setEntity2Type(EntityTypeEnum.PERMISSION_TYPE.getCode());
		relationship.setEntity2Code(e);
		return relationship;
	}
}
